package com.joey.cloud.dic.business.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.joey.cloud.communal.constant.BaseConstant;
import com.joey.cloud.dic.business.entity.SysDicRight;
import com.joey.cloud.dic.business.mapper.SysDicRightMapper;
import com.joey.cloud.dic.business.service.ISysDicRightService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 数据字典右表  服务实现类
 * </p>
 *
 * @author Joey
 * @since 2021-03-21
 */
@Service
public class SysDicRightServiceImpl extends ServiceImpl<SysDicRightMapper, SysDicRight> implements ISysDicRightService {
    @Resource
    SysDicRightMapper sysDicRightMapper;

    @Cacheable(value = "SysDicRight",key = "#leftKey+'-'+#dicKeys")
    @Override
    public String queryTextByKey(String leftKey, String dicKeys) {
        if(StrUtil.isEmpty(leftKey) || StrUtil.isEmpty(dicKeys)){
            return null;
        }
        List<String> dicKeyList = StrUtil.split(dicKeys, BaseConstant.SPITS_CHAR1);
        QueryWrapper qw = new QueryWrapper();
        qw.select("dicText");
        qw.eq("leftKey",leftKey);
        qw.in("dicKey",dicKeyList);
        List<SysDicRight> dicList = sysDicRightMapper.selectList(qw);
        List<String> dicTextList = new ArrayList<>(dicList.size());
        for(SysDicRight item : dicList){
            if(!dicTextList.contains(item.getDicText())){
                dicTextList.add(item.getDicText());
            }
        }
        return CollUtil.join(dicTextList,BaseConstant.SPITS_STR1);
    }
}
